var uid = getParameter("uid");
var code=getParameter("code");
var now = new Date();
$(function() {
	if(uid){
	charts(now);
	}else{
		getToken(code);
	}
});
function getToken(code_){
	var urlStr = serverPath + "/server/doctor/diabetes/login.do";
	var jsonStr = 
	    '{'+
	    '	   "requestid":"",'+
	    '	   "requestsource":"diabetes",'+
	    '	   "sessionid":"",'+
	    '	   "uid":"",'+
	    '	   "method":"taipinglogin",'+
	    '	   "version":"1.0",'+
	    '	   "requestlist":'+
	    '	   ['+
	    '	      {'+
	    '	         "code":"'+code_+'"'+
	    '	      }'+
	    '	   ]'+
	    '	}';
	
	UseAjax(urlStr, jsonStr, document.cookie, getUid, '1');

};

function getUid(data) {
	if(data.resultcode=='0'){
		uid = data.uid;
		charts(now);
	}else{
		alert(data.resultmsg);	
	}
}
function charts(now) {
	var urlStr = serverPath + "/app/doctor/diabetes/weightView.do"; 
	 // var jsonStr ='{method:"showWeightChart",UId:"' + UId + '",date:"' + now.format("yyyy-MM-dd") + '",day:"1"}';
	  var jsonStr = 
		    '{'+
		    '	   "requestid":"",'+
		    '	   "requestsource":"diabetes",'+
		    '	   "sessionid":"",'+
		    '	   "uid":"'+uid+'",'+
		    '	   "method":"showWeightChart",'+
		    '	   "version":"1.0",'+
		    '	   "requestlist":'+
		    '	   ['+
		    '	      {'+
		    '	         "uid":"'+uid+'",'+
		    '            "date":"' + now.format("yyyy-MM-dd") +'",'+
		    '            "day":"1"'+
		    '	      }'+
		    '	   ]'+
		    '	}';
	  UseAjax(urlStr, jsonStr, document.cookie, rt, '1');

};
function rt(data) {
	var wi = data.resultlist[0].records;
	var datas = new Array();
	if(wi.length > 0){
		for (var int = 0; int < wi.length; int++) {
			datas[int]=[Date.UTC(wi[int].dates.substring(0, 4),wi[int].dates.substring(5, 7)-1,wi[int].dates.substring(8, 10),wi[int].dates.substring(11, 13)),Number(wi[int].value)];
		}
		}
	$('#container').highcharts(
			{
				title : {
					text : ''
				},
				subtitle : {
					text : ''//now.format("yyyy-MM-dd")
				},
				xAxis : {
					 type : 'datetime',
					  dateTimeLabelFormats : {
						  minute : '%H:%M',
						    hour : '%H:%M',
						    day : '%m-%d',
						    week : '%m-%d',
						    month : '%m',
						    year : '%Y' 
						  }, 
					maxPadding : 5,
					gridLineWidth : 1,
					gridLineDashStyle : 'Dash',// 样式 虚线
					min : Date.UTC(now.getFullYear(), now.getMonth(), now
							.getDate(),0),
		        	max : Date.UTC(now.getFullYear(), now.getMonth(), now
										.getDate(),24)
	
				},
				yAxis : {
					min : 20, // 固定列最小值
					max : 150,
					title : {
						//align: 'high',
		                //offset: 10,
		                text: 'KG'
		                //rotation: 0,
		                //y: 20
					},labels : {
						formatter : function() {
							return "" + this.value ;
						}
					} 
					
				},
				tooltip : {
					formatter : function() {
					}
				},
				plotOptions : {
					line : {
						dataLabels : {
							enabled : true
						},
						marker: {
		                    enabled: true
		                },
						enableMouseTracking : false
					}
				},
				credits : {
					enabled : false
				},
				legend : {
					enabled : false
				},
				series : [ {
					pointPadding : 0,// 数据点之间的距离值
					groupPadding : 0,// 分组之间的距离值
					name : null,
					data : datas
				} ]
			});
}

$(function() {
	// swipe 的dome
	$("#container").swipe(
			{
				swipeLeft : function(event, direction, distance, duration,
						fingerCount) {

					now = getNextDay(now, 1);
					charts(now);
				},
				swipeRight : function(event, direction, distance, duration,
						fingerCount) {
					now = getLastDay(now, 1);
					charts(now);
				}
			});

});
